<?php

/******************************************************************

	Name: GD 处理函数库
	Author: 徐立 ( Eks Tsui )
	Email: ekstsui@gmail.com
	QQ: 1181601

/******************************************************************/

/*
$Params = array
	Image => 图片地址
	Target => 缩略图保存地址
	MaxWidth => 缩略图最大宽度
	MaxHeight => 缩略图最大高度
	Type => 缩略方式：0=高宽等比缩放 / 1=裁减
*/
function _do_thumb ( $image, $target, $maxWidth, $maxHeight, $type = 0 )
{
	if ( !$data = @GetImageSize ( $image ) ) return false;

	$srcX = $srcY = $srcW = $srcH = 0;

	switch ( intval ( $type ) )
	{
		case 0: // 高宽等比缩放

			$width = $data[0];
			$height = $data[1];

			if ( $data[1] > $maxHeight )
			{
				$width = intval ( $width * $maxHeight / $height );
				$height = $maxHeight;
			}

			if ( $width > $maxWidth )
			{
				 $height = intval ( $height * $maxWidth / $width );
				 $width = $maxWidth;
			}

			if ( $data[0] < $maxWidth && $data[1] < $maxHeight )
			{
				$width = $data[0];
				$height = $data[1];
			}
			break;

		case 1: // 裁减

			$width = $maxWidth;
			$height = $maxHeight;

			if ( $data[0] > $data[1] )
			{
				$srcX = ( $data[0] - $data[1] ) / 2;
				$srcH = $data[1];
				$srcW = intval ( $data[1] * $height / $width );
			}
			else
			{
				 $srcY = ( $data[1] - $data[0] ) / 2;
				 $srcW = $data[0];
				 $srcH = intval ( $data[0] * $width / $height );
			}
	}

	_make_thumb ( $image, $target, $width, $height, $srcX, $srcY, $srcW, $srcH );
}

// 图片处理函数
/*
$Params = array
	srcFile => 源图片地址
	dstFile => 目标图片地址
	dstW => 目标图片宽度
	dstH => 目标图片高度
	srcX => 源图片截取点 X 坐标
	srcY => 源图片截取点 Y 坐标
*/
function _make_thumb ( $srcFile, $dstFile, $dstW, $dstH, $srcX = 0, $srcY = 0, $srcW = 0, $srcH = 0 )
{
	if ( !$data = @GetImageSize ( $srcFile ) ) return false;

	switch ( $data[2] )
	{
		case 1:
			$im = ImageCreateFromGIF ( $srcFile );
			$OutFunc = "ImageGif";
			break;
		case 2:
			$im = ImageCreateFromJPEG ( $srcFile );
			$OutFunc = "ImageJpeg";
			break;
		case 3:
			$im = ImageCreateFromPNG ( $srcFile );
			$OutFunc = "ImagePng";
			break;
		case 6:
			$im = ImageCreateFromWBMP ( $srcFile );
			$OutFunc = "ImageWbmp";
            break;
	}
	$srcW = $srcW ? $srcW : @ImageSX ( $im );
	$srcH = $srcH ? $srcH : @ImageSY ( $im );
	$ni = @ImageCreateTruecolor ( $dstW, $dstH );
	$bgColor = @ImageColorAllocateAlpha ( $ni, 255, 255, 255, 127 );
	@ImageFill ( $ni, 0, 0, $bgColor );
	// ImageColorOpacity ( $ni, $bgColor );

	@ImageCopyResampled ( $ni, $im, 0, 0, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH );
	ob_start ();
	@$OutFunc ( $ni, $dstFile, 90 );
	ob_clean ();
}

// 图片加水印
function _do_watermark ( $srcFile, $dstFile, $Opacity = 100, $Quality = 100, $Position = 0 )
{
	$s_data = GetImageSize ( $srcFile );
	switch ( $s_data[2] )
	{
		case 1:
			$s_im = ImageCreateFromGIF ( $srcFile );
			break;
		case 2:
			$s_im = ImageCreateFromJPEG ( $srcFile );
			break;
		case 3:
			$s_im = ImageCreateFromPNG ( $srcFile );
			break;
		case 6:
			$s_im = ImageCreateFromWBMP ( $srcFile );
            break;
	}
	$d_data = GetImageSize ( $dstFile );
	switch ( $d_data[2] )
	{
		case 1:
			$im = ImageCreateFromGIF ( $dstFile );
			$OutFunc = "ImageGif";
			break;
		case 2:
			$im = ImageCreateFromJPEG ( $dstFile );
			$OutFunc = "ImageJpeg";
			break;
		case 3:
			$im = ImageCreateFromPNG ( $dstFile );
			$OutFunc = "ImagePng";
			break;
		case 6:
			$im = ImageCreateFromWBMP ( $dstFile );
			$OutFunc = "ImageWbmp";
            break;
	}
	$padding = 5;

	switch ( $Position )
	{
		case 7: // 顶端居左
		{
			$x = $padding;
			$y = $padding;
			break;
		}
		case 8: // 顶端居中
		{
			$x = $d_data[0] / 2 - $s_data[0] / 2;
			$y = $padding;
			break;
		}
		case 9: // 顶端居右
		{
			$x = $d_data[0] - $s_data[0] - $padding;
			$y = $padding;
			break;
		}
		case 4: // 中间居左
		{
			$x = $padding;
			$y = $d_data[1] / 2 - $s_data[1] / 2;
			break;
		}
		case 6: // 中间居右
		{
			$x = $d_data[0] - $s_data[0] - $padding;
			$y = $d_data[1] / 2 - $s_data[1] / 2;
			break;
		}
		case 1: // 底部居左
		{
			$x = $padding;
			$y = $d_data[1] - $s_data[1] - $padding;
			break;
		}
		case 2: // 底部居中
		{
			$x = $d_data[0] / 2 - $s_data[0] / 2;
			$y = $d_data[1] - $s_data[1] - $padding;
			break;
		}
		case 3: // 底部居右
		{
			$x = $d_data[0] - $s_data[0] - $padding;
			$y = $d_data[1] - $s_data[1] - $padding;
		}
		default: // 绝对居中
		{
			$x = $d_data[0] / 2 - $s_data[0] / 2;
			$y = $d_data[1] / 2 - $s_data[1] / 2;
		}
	}

	if ( function_exists ( "ImageCopyMerge" ) )
	{
		ImageCopyMerge ( $im, $s_im, $x, $y, 0, 0, $s_data[0], $s_data[1], $Opacity );
	}
	else
	{
		ImageCopy ( $im, $s_im, $x, $y, 0, 0, $s_data[0], $s_data[1] );
	}
	$OutFunc ( $im, $dstFile, $Quality );
}



// 生成统计图

function create_img(&$score,&$di,$per_w=30,$per_h=20,$per_n=10,$per_p=1)
//参数：数组；数组；单元格宽度；单元格高度；单元格在垂直方向上的数目；每象素代表的数值
{
    header("Content-type: image/png");
    header("Pragma: no-cache");
    $number=count($score);
    $img_w=($number+2)*$per_w;
    $img_h=$per_h*$per_n;
    $img_w_t=$img_w;
    $img_h_t=$img_h+30;
    $image=imagecreate($img_w_t,$img_h_t);

    $color_back=imagecolorallocate($image,0,0,0);
    $color_table=imagecolorallocate($image,188,188,188);
    $color_pole_b=imagecolorallocate($image,204,0,255);
    $color_pole_f=imagecolorallocate($image,152,0,188);
    $color_pole_s=imagecolorallocate($image,231,132,255);
    $color_text=imagecolorallocate($image,255,217,38);
 //配置颜色

    imagefilledrectangle($image,0,0,$img_w_t,$img_h_t,$color_back);
//填充背景

    $style=array($color_table,$color_table,$color_back,$color_back,$color_back,$color_back);
    imagesetstyle($image, $style);
    for($i=0;$i<$img_h/$per_h;$i++)
    {
      imageline($image,$per_w,$i*$per_h,$img_w-$per_w,$i*$per_h,IMG_COLOR_STYLED);
      imageline($image,$per_w,$i*$per_h,$per_w*1/2,$i*$per_h+$per_h*2/3-1,IMG_COLOR_STYLED);
      $str_score=$per_h*($img_h/$per_h-$i-1)*$per_p;
      imagestring($image,3,$img_w-$per_w+6,$i*$per_h-11,$str_score,$color_table);
    }
    imageline($image,$per_w,0,$img_w-$per_w,0,$color_table);
    imageline($image,$per_w,$per_h*($per_n-1),$img_w-$per_w,$per_h*($per_n-1),$color_table);
    imageline($image,$per_w*1/2,$img_h-$per_h*1/3-1,$img_w-$per_w*3/2,$img_h-$per_h*1/3-1,$color_table);
    imageline($image,$per_w*1/2,$per_h*2/3,$per_w*1/2,$img_h-$per_h*1/3-1,$color_table);
    imageline($image,$per_w,0,$per_w*1/2,$per_h*2/3-1,$color_table);
    imageline($image,$per_w,$per_h*($per_n-1),$per_w*1/2,$img_h-$per_h*1/3-1,$color_table);
    imageline($image,$img_w-$per_w,$per_h*($per_n-1),$img_w-$per_w*1/2-$per_w,$img_h-$per_h*1/3-1,$color_table);
    imageline($image,$img_w-$per_w,0,$img_w-$per_w,$img_h-$per_h,$color_table);
    imageline($image,$per_w,0,$per_w,$img_h-$per_h,$color_table);
//画网格

    for ($i=0;$i<$number;$i++)
    {
    $pn=array(($i+1)*$per_w,$img_h-$score[$i]/$per_p-$per_h*2/3,($i+1)*$per_w,$img_h-$per_h*2/3-1,($i+2)*$per_w-$per_w/4-1,$img_h-1-$per_h*2/3,($i+2)*$per_w-1,$img_h-1-$per_h,($i+2)*$per_w-1,$img_h-$score[$i]/$per_p-$per_h+1,($i+1)*$per_w+$per_w/4,$img_h-$score[$i]/$per_p-$per_h+1);
    imagefilledpolygon($image,$pn,6,$color_pole_b);
    $ps=array(($i+1)*$per_w,$img_h-$score[$i]/$per_p-$per_h*2/3,($i+2)*$per_w-$per_w/4-1,$img_h-$score[$i]/$per_p-$per_h*2/3,($i+2)*$per_w-2,$img_h-$score[$i]/$per_p-$per_h+1,($i+1)*$per_w+$per_w/4,$img_h-$score[$i]/$per_p-$per_h+1);
    imagefilledpolygon($image,$ps,4,$color_pole_s);
    imagefilledrectangle($image,($i+1)*$per_w,$img_h-$score[$i]/$per_p-$per_h*2/3+1,($i+2)*$per_w-$per_w/4-1,$img_h-$per_h*2/3-1,$color_pole_f);
//画柱形图

    $len=strlen($score[$i]);
    $p_x=($i*$per_w)+$per_w+($per_w-$len*5-3)/2;
//使成绩在立柱中间
    imagestring($image,2,ceil($p_x),$img_h-$score[$i]/$per_p-1.6*$per_h,$score[$i],$color_text);
//写入成绩在柱状图上方
    }
for($i=0;$i<$number;$i++)
{
    $st=base64_decode($di[$i]);
	$str = $st;
  //  $str=iconv("gb2312","UTF-8",$st);
//转换中文代码为UTF-8，不然中文会出现乱码，需要iconv模块的支持

    imagettftext($image,9,270,$per_w*($i+2)-$per_w/2-8,$img_h+5,$color_table,"simkai.ttf",$str);
}
    imagepng($image);
    imagedestroy($image);
}
?>